﻿using CMS.Models;
using CMS.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MahApps.Metro.Controls.Dialogs;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class MemberViewModel : ViewModelBase
    {

        public AppData AppData { get; set; } = AppData.Instance;

        public MemberViewModel()
        {
            // 刷新数据
            Members = new MemberProvider().Select();
        }

        private List<Member> members = new List<Member>();
        /// <summary>
        /// 所有物资
        /// </summary>
        public List<Member> Members
        {
            get
            {
                return members;
            }
            set
            {
                this.members = value;
                RaisePropertyChanged();
            }
        }


       

        /// <summary>
        /// 打开添加物资对话框
        /// </summary>
        public RelayCommand OpenAddMemberWindowCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var window = new AddMemberWindow();
                    window.ShowDialog();
                    // 刷新数据
                    Members = new MemberProvider().Select();
                });
            }
        }

        /// <summary>
        /// 删除物资命令
        /// </summary>
        public RelayCommand<object> DeleteMemberCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (MessageBox.Show("是否删除当前选定的项目？", "是否确认", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
                    {
                        if (!(arg is Member model)) return;
                        var count = new MemberProvider().Delete(model);
                        if (count > 0) AppData.Instance.MainWindow.ShowMessageAsync("删除成功", $"成功删除，{model.Name}");
                        // 刷新数据
                        Members = new MemberProvider().Select();
                    }

                });
            }
        }
        /// <summary>
        /// 修改物资命令
        /// </summary>
        public RelayCommand<object> EditMemberCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (!(arg is Member model)) return;
                    var count = new MemberProvider().Update(model);
                    if (count > 0) AppData.Instance.MainWindow.ShowMessageAsync("操作成功", $"修改成功，{model.Name}");
                    // 刷新数据
                    Members = new MemberProvider().Select();
                });
            }
        }

    }
}
